<?php

class CursosController extends Zend_Controller_Action
{

    protected $_cursos = null;
    
    public function init()
    {
        /* Initialize action controller here */
        $this->view->baseUrl = $this->_request->getBaseUrl();
        $this->view->cliente = Zend_Auth::getInstance()->getIdentity();
        $this->_cursos = new Application_Model_DbTable_Cursos();
    }

    public function crearAction()
    {
        // action body
        $id_carrera = $this->_getParam('carrera');
        
        $form = new Application_Form_Curso();
        $form->setAction('/cursos/crear/carrera/'.$id_carrera);
        $form->setMethod('post');
        
        if( $this->getRequest()->isPost() ){
            if( $form->isValid( $this->_getAllParams() ) ) {
                $nombre = $form->getValue('nombre');
                $descripcion = $form->getValue('descripcion');
                $this->_cursos->crearCurso($nombre,$descripcion,$id_carrera);
                $this->_redirect('/carreras', array('method','get')); 
            }      
            $this->view->form = $form;    
        }elseif( $this->getRequest()->isGet() ){
            $this->view->form = $form;
        }
        
    }

    public function editarAction()
    {
        // action body
        $id = $this->_getParam('id');
        
        $form = new Application_Form_Curso();
        $form->setAction('/cursos/editar/id/'.$id);
        $form->setMethod('post');
        
        if( $this->getRequest()->isPost() ){
            if( $form->isValid( $this->_getAllParams() ) ) {
                $nombre = $form->getValue('nombre');
                $descripcion = $form->getValue('descripcion');
                $this->_cursos->editarCurso($id,$nombre,$descripcion);
                $this->_redirect('/carreras', array('method','get')); 
            }      
            $this->view->form = $form;    
        }elseif( $this->getRequest()->isGet() ){
            $curso = $this->_cursos->obtener_curso($id);
            $form->inicializar($curso);
            $this->view->form = $form;
        }
    }

    public function borrarAction()
    {
        // action body
        $id = $this->_getParam('id');
        $this->_cursos->borrarCurso($id);
        $this->_redirect('/carreras', array('method','get')); 
    }

}

